/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

/**
 * Level class that contains arrays for positioning and storing all objects in
 * the level.
 */
public class Level {

    /**
     * gets the current level.
     *
     * @return number level
     */
    public final int getCurrUsage() {
        return currUsage;
    }

    /**
     * sets the current level.
     *
     * @param currUsageX the level number.
     */
    public final void setCurrUsage(final int currUsageX) {
        this.currUsage = currUsageX;
    }

    /**
     * gets the length of the array.
     *
     * @return the length.
     */
    public final int getArrayLength() {
        return arrayLength;
    }
    /**
     * Number used to determine what level is being used.
     */
    private int currUsage;
    /**
     * Length of the arrays.
     */
    private final int arrayLength = 40;
    /**
     * X variables array.
     */
    private int[][] gridNumX = new int[arrayLength][arrayLength];
    /**
     * Y variables array.
     */
    private int[][] gridNumY = new int[arrayLength][arrayLength];
    /**
     * Array for level objects.
     */
    private LevelObject[][] grid = new LevelObject[arrayLength][arrayLength];
    /**
     * Initial x value.
     */
    private final int initX = 10;
    /**
     * Initial y value.
     */
    private final int initY = 10;
    /**
     * Increase value.
     */
    private final int inc = 20;

    /**
     * Constructor that sets all the positions of the arrays.
     *
     * @param currUsageX = current level.
     */
    public Level(final int currUsageX) {
        currUsage = currUsageX;
        int x1;
        int y1 = initY;
        for (int x = 0; x < arrayLength; x = x + 1) {
            x1 = initX;
            for (int y = 0; y < arrayLength; y = y + 1) {
                gridNumX[x][y] = x1;
                x1 = x1 + inc;
            }
        }
        for (int x = 0; x < arrayLength; x = x + 1) {
            for (int y = 0; y < arrayLength; y = y + 1) {
                gridNumY[x][y] = y1;
                if (y == arrayLength - 1) {
                    y1 = y1 + inc;
                }
            }
        }
        for (int x = 0; x < arrayLength; x = x + 1) {
            for (int y = 0; y < arrayLength; y = y + 1) {
                grid[x][y] = new Background();
                grid[x][y].setX(gridNumX[x][y]);
                grid[x][y].setY(gridNumY[x][y]);
            }
        }
    }

    /**
     * Method that adds an object to the level supplied at the x and y of the
     * grid array.
     *
     * @param x the x position.
     * @param y the Y position.
     * @param l The level.
     */
    public final void addLevelObject(final int x,
            final int y, final LevelObject l) {
        grid[x][y] = l;
        l.setX(gridNumX[x][y]);
        l.setY(gridNumY[x][y]);
        l.setArrayX(x);
        l.setArrayY(y);
    }

    /**
     * Returns the level objects at the specified position of the grid.
     *
     * @param y the y position.
     * @param x the x position.
     * @return the level object.
     */
    public final LevelObject getGrid(final int y, final int x) {
        return grid[y][x];
    }

    /**
     * Sets a grid position to an object.
     *
     * @param y y coordinate.
     * @param x x coordinate.
     */
    public final void setGrid(final int y, final int x) {
        grid[y][x] = null;
    }

    /**
     * Goes through the array and finds the x position for the hero class.
     *
     * @return the x position.
     */
    public final int heroX() {
        try {
            for (int i = 0; i < arrayLength; i = i + 1) {
                for (int k = 0; k < arrayLength; k = k + 1) {
                    if (grid[i][k].getClass().equals(Hero.class)) {
                        return k;
                    }
                }
            }
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Funkar ej array.");

        }
        return 0;
    }

    /**
     * Finds the y position of the hero class in the array.
     *
     * @return the y position.
     */
    public final int heroY() {
        try {
            for (int i = 0; i < arrayLength; i = i + 1) {
                for (int k = 0; k < arrayLength; k = k + 1) {
                    if (grid[i][k].getClass().equals(Hero.class)) {
                        return i;
                    }
                }
            }
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Funkar ej array.");

        }
        return 0;
    }
}
